// require("@/http/ie9")
import $ from "jquery";
import BaseUrl from "./uploadconfig";
import {
  Notification,
  MessageBox
} from 'element-ui'
import router from '@/router/index'
jQuery.support.cors = true;

function ajax(url, method = 'GET', data, cb, errfn, boo) {
  method === 'POST' ? data = JSON.stringify(data) : '';
  let header = {};
  if (!boo) {
    header['Authorization'] = sessionStorage.token ? 'Bearer ' + sessionStorage.token : '';
  }
  $.ajax({
    url: BaseUrl.baseurl + url,
    data: data ? data : '',
    headers: header,
    contentType: "application/json",
    type: method,
    success: res => {
      cb(res)
    },
    error: function (err) {
      if (err.responseJSON.status === 401) {
        router.push('/login')
        // MessageBox.confirm(
        //   '登录状态已过期，您可以继续留在该页面，或者重新登录',
        //   '系统提示', {
        //     confirmButtonText: '重新登录',
        //     cancelButtonText: '取消',
        //     type: 'warning'
        //   }
        // ).then(() => {
        //   router.push('/login')
        //   // location.reload()
        //   // setTimeout(() => {
        //   //   location.reload() // 为了重新实例化vue-router对象 避免bug
        //   // }, 200)
        // })
      } else {
        Notification.error({
          title: err.responseJSON.message,
          duration: 2000
        })
      }
      errfn(err.responseJSON)
    }
  });
}

export const _getAddress = (keyword, cb, er) => { // 获取地址
  // $.ajax({
  //   url: 'http://restapi.amap.com/v3/config/district',
  //   data: {
  //     key: 'ffb08209d403b98295cff1f4309f0652',
  //     subdistrict: 1,
  //     extensions: 'base',
  //     keywords: keyword
  //   },
  //   contentType: "application/json",
  //   type: 'GET',
  //   success: res => {
  //     cb(res)
  //   },
  //   error: err => {
  //     er(err.responseJSON)
  //   }
  // });
  ajax(`/api/districts/all`, 'GET', {
    parentId: keyword
  }, cb, er, true)
}

export const _location = (data, cb, er) => { // 获取地址 经纬度
  ajax(`/api/get/location`, 'POST', data, cb, er)
}
export const _vCode = (cb, er) => { // 获取验证码
  ajax(`/auth/vCode`, 'GET', '', cb, er, true)
}
export const _login = (data, cb, er) => { // 登录
  ajax(`/auth/login`, 'POST', data, cb, er, true)
}
export const _mineInfo = (data, cb, er) => { // 获取用户信息
  ajax(`/auth/info`, 'GET', data, cb, er)
}
export const _userLevel = (id, cb, er) => { // 获取用户等级
  ajax(`/api/users/show/${id}`, 'GET', {}, cb, er)
}

export const _getCode = (data, cb, er) => { // 获取验证码
  ajax(`/api/sms/getCode`, 'POST', data, cb, er, true)
}
export const _register = (data, cb, er) => { // 注册
  ajax(`/api/users/register`, 'POST', data, cb, er, true)
}
export const _forgot = (data, cb, er) => { // 忘记密码
  ajax(`/api/users/updatePass`, 'POST', data, cb, er, true)
}
export const _forgotnew = (data, cb, er) => { // 忘记密码
  ajax(`/api/users/reset`, 'POST', data, cb, er, true)
}

export const _dictGet = (data, cb, er) => { // 获取菜单tree
  ajax(`/api/dictDetail?dictName=job_status`, 'GET', data, cb, er)
}
export const _excelExport = (data, cb, er) => { 
  ajax(`/api/excel/export`, 'GET', data, cb, er)
}
export const _excelData = (data, cb, er) => { // 导出excel
  ajax(`/api/excel/data`, 'GET', data, cb, er)
}


// 菜单
export const _menusBuild = (data, cb, er) => { // 获取谋个用户菜单
  ajax(`/api/menus/build`, 'GET', data, cb, er)
}
export const _menusList = (cb, er) => { // 获取菜单
  ajax(`/api/menus/getmenu/list`, 'GET', {}, cb, er)
}
export const _menusUpdate = (data, cb, er) => { // 修改菜单
  ajax(`/api/menus/update`, 'POST', data, cb, er)
}
export const _menusCreate = (data, cb, er) => { // 新加菜单
  ajax(`/api/menus/create`, 'POST', data, cb, er)
}
export const _menusDelete = (id, cb, er) => { // 删除行业
  ajax(`/api/menus/delete/${id}`, 'POST', {}, cb, er)
}
export const _menusAll = (cb, er) => { // 获取菜单tree
  ajax(`/api/menus/tree`, 'GET', {}, cb, er)
}


// 网关
export const _gatewaysGet = (data, cb, er) => { // 获取网关列表
  ajax(`/api/equipmentGateways/list`, 'GET', data, cb, er)
}
export const _gatewaysList = (data, cb, er) => { // 获取网关列表 (用户端)
  ajax(`/api/chart/gateway/client`, 'POST', data, cb, er)
}
export const _gatewaysAllGet = (data,cb, er) => { // 获取简易网关列表
  ajax(`/api/equipmentGateways/all`, 'GET', data, cb, er)
}

export const _gatewaysShow = (id, cb, er) => { // 获取网关详情
  ajax(`/api/equipmentGateways/show/${id}`, 'GET', {}, cb, er)
}
export const _gatewaysShowNew = (data, cb, er) => { // 获取网关详情 new
  ajax(`/api/chart/report/gateways`, 'POST', data, cb, er)
}



// 设备 关联用户
export const _eqmtMembersList = (data, cb, er) => {
  ajax(`/api/equipments/members/${data.id}`, 'GET', {}, cb, er)
}
export const _eqmtMembersDelete = (data, cb, er) => {
  ajax(`/api/equipments/delete/member`, 'POST', data, cb, er)
}
export const _eqmtMembersAdd = (data, cb, er) => {
  ajax(`/api/equipments/set/member`, 'POST', data, cb, er)
}
// 设备
export const _eqmtLatLng = (data, cb, er) => { // 设备经纬度
  ajax(`/api/equipments/latLng`, 'POST', data, cb, er)
}
export const _equipmentGet = (data, cb, er) => { // 获取设备列表
  ajax(`/api/equipments/list`, 'GET', data, cb, er)
}
export const _eqmtListOrder = (data, cb, er) => { // 获取订单绑定 的设备列表
  ajax(`/api/equipments/order`, 'POST', data, cb, er)
}
export const _eqmtList = (data, cb, er) => { // 获取设备列表 (用户端)
  ajax(`/api/chart/equipment/client`, 'POST', data, cb, er)
}

export const _equipmentShow = (id, cb, er) => { // 获取设备详情
  ajax(`/api/equipments/show/${id}`, 'GET', {}, cb, er)
}
export const _equipmentCreate = (data, cb, er) => { // 新建设备
  ajax(`/api/equipments/create`, 'POST', data, cb, er)
}
export const _equipmentUpdate = (data, cb, er) => { // 更新设备
  ajax(`/api/equipments/update`, 'POST', data, cb, er)
}
export const _equipmentModify = (data, cb, er) => {
  ajax(`/api/equipments/modify`, 'POST', data, cb, er)
}


// 行业
export const _industryGet = (data, cb, er) => { // 获取行业列表
  ajax(`/api/industry/list`, 'GET', data, cb, er)
}
export const _industryAllGet = (cb, er) => { // 获取简易行业列表
  ajax(`/api/industry/all`, 'GET', {}, cb, er)
}
export const _industryGetById = (id, data, cb, er) => { // 获取行业详情
  ajax(`/api/industry/show/${id}`, 'GET', data, cb, er)
}
export const _industryCreate = (data, cb, er) => { // 创建行业
  ajax(`/api/industry/create`, 'POST', data, cb, er)
}
export const _industryDelete = (id, cb, er) => { // 删除行业
  ajax(`/api/industry/delete/${id}`, 'POST', {}, cb, er)
}
export const _industryUpdate = (data, cb, er) => { // 更新行业
  ajax(`/api/industry/update`, 'POST', data, cb, er)
}

// imei
export const _findImei = (data, cb, er) => { // 判断imei是否有效
  ajax(`/api/equipments/find/imei`, 'POST', data, cb, er)
}

// 文章
export const _articleList = (data, cb, er) => { // 获取文章列表
  ajax(`/api/articles/list`, 'GET', data, cb, er)
}
export const _articleCreate = (data, cb, er) => { // 创建文章
  ajax(`/api/articles/create`, 'POST', data, cb, er)
}
export const _articleDelete = (id, cb, er) => { // 删除文章
  ajax(`/api/articles/delete/${id}`, 'POST', {}, cb, er)
}
export const _articleUpdate = (data, cb, er) => { // 更新文章
  ajax(`/api/articles/update`, 'POST', data, cb, er)
}
export const _articleShow = (id, cb, er) => { // 获取文章详情
  ajax(`/api/articles/show/${id}`, 'GET', {}, cb, er)
}


// 小程序
export const _bannerList = (data, cb, er) => { // banner列表
  ajax(`/api/banner/list`, 'GET', data, cb, er)
}
export const _bannerCreate = (data, cb, er) => { // 创建banner
  ajax(`/api/banner/create`, 'POST', data, cb, er)
}
export const _bannerDelete = (id, cb, er) => { // 删除banner
  ajax(`/api/banner/delete/${id}`, 'POST', {}, cb, er)
}
export const _bannerUpdate = (data, cb, er) => { // 更新banner
  ajax(`/api/banner/update`, 'POST', data, cb, er)
}



// 报警设置
export const _reportList = (data, cb, er) => { // report列表
  ajax(`/api/reportSetting/list`, 'GET', data, cb, er)
}
export const _reportCreate = (data, cb, er) => { // 创建report
  ajax(`/api/reportSetting/create`, 'POST', data, cb, er)
}
export const _reportDelete = (id, cb, er) => { // 删除report
  ajax(`/api/reportSetting/delete/${id}`, 'POST', {}, cb, er)
}
export const _reportUpdate = (data, cb, er) => { // 更新report
  ajax(`/api/reportSetting/update`, 'POST', data, cb, er)
}

// 工单设置orderCategory
export const _orderCategoryAll = (cb, er) => { // orderCategory列表
  ajax(`/api/orderCategory/all`, 'GET', {}, cb, er)
}
export const _orderCategoryList = (data, cb, er) => { // orderCategory列表
  ajax(`/api/orderCategory/list`, 'GET', data, cb, er)
}
export const _orderCategoryCreate = (data, cb, er) => { // 创建orderCategory
  ajax(`/api/orderCategory/create`, 'POST', data, cb, er)
}
export const _orderCategoryDelete = (id, cb, er) => { // 删除orderCategory
  ajax(`/api/orderCategory/delete/${id}`, 'POST', {}, cb, er)
}
export const _orderCategoryUpdate = (data, cb, er) => { // 更新orderCategory
  ajax(`/api/orderCategory/update`, 'POST', data, cb, er)
}

// 工作流设置workflows
export const _workflowsList = (data, cb, er) => { // workflows列表
  ajax(`/api/workflows/list`, 'GET', data, cb, er)
}
export const _workflowsCreate = (data, cb, er) => { // 创建workflows
  ajax(`/api/workflows/create`, 'POST', data, cb, er)
}
export const _workflowsDelete = (id, cb, er) => { // 删除workflows
  ajax(`/api/workflows/delete/${id}`, 'POST', {}, cb, er)
}
export const _workflowsUpdate = (data, cb, er) => { // 更新workflows
  ajax(`/api/workflows/update`, 'POST', data, cb, er)
}

// 部门 （组织架构）设置dept
export const _deptList = (data, cb, er) => { // dept列表
  ajax(`/api/dept/list`, 'GET', data, cb, er)
}
export const _deptCreate = (data, cb, er) => { // 创建dept
  ajax(`/api/dept/create`, 'POST', data, cb, er)
}
export const _deptDelete = (id, cb, er) => { // 删除dept
  ajax(`/api/dept/delete/${id}`, 'POST', {}, cb, er)
}
export const _deptUpdate = (data, cb, er) => { // 更新dept
  ajax(`/api/dept/update`, 'POST', data, cb, er)
}

// 权限 permissions
export const _permissionsList = (data, cb, er) => { // permissions列表
  ajax(`/api/permissions/list`, 'GET', data, cb, er)
}
export const _permissionsCreate = (data, cb, er) => { // 创建permissions
  ajax(`/api/permissions/create`, 'POST', data, cb, er)
}
export const _permissionsDelete = (id, cb, er) => { // 删除permissions
  ajax(`/api/permissions/delete/${id}`, 'POST', {}, cb, er)
}
export const _permissionsUpdate = (data, cb, er) => { // 更新permissions
  ajax(`/api/permissions/update`, 'POST', data, cb, er)
}
export const _permissionsAll = (cb, er) => { // 简易权限
  ajax(`/api/permissions/tree`, 'GET', {}, cb, er)
}

// 岗位 设置job
export const _jobList = (data, cb, er) => { // job列表
  ajax(`/api/job/list`, 'GET', data, cb, er)
}
export const _jobCreate = (data, cb, er) => { // 创建job
  ajax(`/api/job/create`, 'POST', data, cb, er)
}
export const _jobDelete = (id, cb, er) => { // 删除job
  ajax(`/api/job/delete/${id}`, 'POST', {}, cb, er)
}
export const _jobUpdate = (data, cb, er) => { // 更新job
  ajax(`/api/job/update`, 'POST', data, cb, er)
}
export const _jobAll = (cb, er) => { // 简易岗位
  ajax(`/api/job/all`, 'GET', {}, cb, er)
}

// 角色 设置/api/roles/{id}
export const _rolesShow = (id, cb, er) => { // 谋个角色详情
  ajax(`/api/roles/${id}`, 'GET', {}, cb, er)
}
export const _rolesList = (data, cb, er) => { // roles列表
  ajax(`/api/roles/list`, 'GET', data, cb, er)
}
export const _rolesAll = (data,cb, er) => { // 简易角色
  ajax(`/api/roles/all`, 'GET', data, cb, er)
}
export const _rolesCreate = (data, cb, er) => { // 创建roles
  ajax(`/api/roles/create`, 'POST', data, cb, er)
}
export const _rolesDelete = (id, cb, er) => { // 删除roles
  ajax(`/api/roles/delete/${id}`, 'POST', {}, cb, er)
}
export const _rolesUpdate = (data, cb, er) => { // 更新roles
  ajax(`/api/roles/update`, 'POST', data, cb, er)
}
export const _rolesUpdateMenu = (data, cb, er) => { // 更新roles 关联的菜单
  ajax(`/api/roles/menu/updatemenu`, 'POST', data, cb, er)
}
export const _rolesUpdatePermission = (data, cb, er) => { // 更新roles 关联的权限
  ajax(`/api/roles/permission/updatePermission`, 'POST', data, cb, er)
}


// 订单 申请
export const _ordersModify = (data, cb, er) => { // _ordersModify
  ajax(`/api/orders/modify`, 'POST', data, cb, er)
}
export const _ordersList = (data, cb, er) => { // orders列表
  ajax(`/api/orders/list`, 'GET', data, cb, er)
}
export const _ordersShow = (id, cb, er) => { // orders详情
  ajax(`/api/orders/show/${id}`, 'GET', {}, cb, er)
}
export const _ordersCreate = (data, cb, er) => { // 创建orders
  ajax(`/api/orders/create`, 'POST', data, cb, er)
}
export const _ordersDelete = (id, cb, er) => { // 删除orders
  ajax(`/api/orders/delete/${id}`, 'POST', {}, cb, er)
}
export const _ordersUpdate = (data, cb, er) => { // 更新orders
  ajax(`/api/orders/update`, 'POST', data, cb, er)
}


// 用户
export const _usersList = (data, cb, er) => { // 用户列表
  ajax(`/api/users/list`, 'GET', data, cb, er)
}
export const _usersCreate = (data, cb, er) => { // 创建users
  ajax(`/api/users/create`, 'POST', data, cb, er)
}
export const _usersDelete = (id, cb, er) => { // 删除users
  ajax(`/api/users/delete/${id}`, 'POST', {}, cb, er)
}
export const _usersUpdate = (data, cb, er) => { // 更新users
  ajax(`/api/users/update`, 'POST', data, cb, er)
}

// 报表
export const _chartReport = (data, cb, er) => { //电流
  ajax(`/api/chart/report`, 'POST', data, cb, er, true)
}
// 工单类型
export const _orderCategory = (cb, er) => { 
  ajax(`/api/chart/order/category`, 'GET', {}, cb, er)
}
// 获取用户
export const _chartUsers = (data, cb, er) => { 
  ajax(`/api/chart/get/users`, 'POST', data, cb, er)
}
// 修改用户资料
export const _chartProfile = (data, cb, er) => { 
  ajax(`/api/chart/update/profile`, 'POST', data, cb, er)
}
// 周报
export const _chartReportWeek = (data, cb, er) => { //周报
  ajax(`/api/chart/report/week`, 'POST', data, cb, er)
}
// 周报 new
export const _chartReportWeekNew = (data, cb, er) => { //周报
  ajax(`/api/chart/report/week/new`, 'POST', data, cb, er)
}
// 月报 new
export const _chartReportMonth = (data, cb, er) => { //月报
  ajax(`/api/chart/report/month`, 'POST', data, cb, er)
}
// 首页报表
export const _chartReportIndex = (data, cb, er) => {
  ajax(`/api/chart/report/index`, 'POST', data, cb, er)
}

// mqtt
export const _mqttPub = (data, cb, er) => {
  ajax(`/api/emqtt/sendTopic`, 'POST', data, cb, er, true);
}

// sms
export const _smsList = (data, cb, er) => {
  ajax(`/api/sms/list`, 'GET', data, cb, er);
}

export const _userUpdatePass = (data, cb, er) => {
  ajax(`/api/users/update/pass`, 'POST', data, cb, er);
}


// monitor 系统监控
export const _logsList = (data, cb, er) => {
  ajax(`/api/logs`, 'GET', data, cb, er, true);
}
// monitor 系统监控
export const _errorList = (data, cb, er) => {
  ajax(`/api/logs/error`, 'GET', data, cb, er);
}
// monitor 系统监控
export const _userLogsList = (data, cb, er) => {
  ajax(`/api/logs/user`, 'GET', data, cb, er);
}
// 历史数据
export const _historyAlarm = (data, cb, er) => {
  ajax(`/api/history/alarm`, 'POST', data, cb, er);
}
// 报警数据
export const _historyList = (data, cb, er) => {
  ajax(`/api/history/list`, 'POST', data, cb, er);
}
// 设备关联用户
export const _historyUserList = (data, cb, er) => {
  ajax(`/api/history/user/list`, 'POST', data, cb, er);
}
// 小程序 版本
export const _xiaochengxuPOST = (data, cb, er) => {
  ajax(`/api/version/update`, 'POST', data, cb, er);
}
// 小程序 版本
export const _xiaochengxuGET = (data, cb, er) => {
  ajax(`/api/version/index`, 'GET', data, cb, er);
}
